package com.summer.peony.myinterface.service.impl;

import java.io.IOException;
import java.util.HashMap;

import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.summer.peony.common.utils.HttpRequest;
import com.summer.peony.myinterface.mapper.MyInterfaceMapper;
import com.summer.peony.myinterface.pojo.MyInterface;
import com.summer.peony.myinterface.service.TestBaseService;
import com.sun.el.parser.ParseException;

@Transactional
@Service
public class TestBaseServiceImpl implements TestBaseService {

	@Autowired
	private MyInterfaceMapper myInterfaceMapper;

	@Override
	public Integer executeTest(MyInterface myInterface) throws ClientProtocolException, IOException, ParseException {
		// TODO 执行接口测试
		String method = myInterface.getMethod();
		String address = myInterface.getAddress();
		HttpRequest httpRequest = new HttpRequest();
		HashMap<String, String> headermap = new HashMap<String, String>();
		headermap.put("Content-Type", "application/json;charset=UTF-8");
		if (method.equals("GET")) {
			CloseableHttpResponse response = httpRequest.get("http://59.110.171.91:8082/" + address);
			int code = httpRequest.getStatusCode(response);
			myInterface.setActualResults(httpRequest.getResponseJson(response).toJSONString());
			if (code == 200) {
				myInterface.setResult(1); // 执行成功
				int key = myInterfaceMapper.updateByPrimaryKey(myInterface);
				if (key != 1) {
					return 2;
				}
				return code;
			}
			myInterface.setResult(2); // 执行失败
			int key = myInterfaceMapper.updateByPrimaryKey(myInterface);
			if (key != 1) {
				return 2;
			}
			return code;
		} else if (method.equals("POST")) {
			String body = myInterface.getBody();
			CloseableHttpResponse response = httpRequest.post("http://59.110.171.91:8082/" + address, body, headermap);
			int code = httpRequest.getStatusCode(response);
			myInterface.setActualResults(httpRequest.getResponseJson(response).toJSONString());
			if (code == 200) {
				myInterface.setResult(1); // 执行成功
				int key = myInterfaceMapper.updateByPrimaryKey(myInterface);
				if (key != 1) {
					return 2;
				}
				return code;
			}
			myInterface.setResult(2); // 执行失败
			int key = myInterfaceMapper.updateByPrimaryKey(myInterface);
			if (key != 1) {
				return 2;
			}
			return code;
		} else if (method.equals("PUT")) {
			String body = myInterface.getBody();
			CloseableHttpResponse response = httpRequest.put("http://59.110.171.91:8082/" + address, body, headermap);
			int code = httpRequest.getStatusCode(response);
			myInterface.setActualResults(httpRequest.getResponseJson(response).toJSONString());
			if (code == 200) {
				myInterface.setResult(1); // 执行成功
				int key = myInterfaceMapper.updateByPrimaryKey(myInterface);
				if (key != 1) {
					return 2;
				}
				return code;
			}
			myInterface.setResult(2); // 执行失败
			int key = myInterfaceMapper.updateByPrimaryKey(myInterface);
			if (key != 1) {
				return 2;
			}
			return code;
		} else if (method.equals("DELETE")) {
			CloseableHttpResponse response = httpRequest.delete("http://59.110.171.91:8082/" + address);
			int code = httpRequest.getStatusCode(response);
			myInterface.setActualResults(httpRequest.getResponseJson(response).toJSONString());
			if (code == 200) {
				myInterface.setResult(1); // 执行成功
				int key = myInterfaceMapper.updateByPrimaryKey(myInterface);
				if (key != 1) {
					return 2;
				}
				return code;
			}
			myInterface.setResult(2); // 执行失败
			int key = myInterfaceMapper.updateByPrimaryKey(myInterface);
			if (key != 1) {
				return 2;
			}
			return code;
		} else {
			return 0;
		}
	}

}
